package com.tedu.ds.workRecord.service.impl;

import com.tedu.ds.employee.mapper.EmployeeMapper;
import com.tedu.ds.employee.pojo.dto.EmployeeQuery;
import com.tedu.ds.employee.pojo.vo.EmployeeVO;
import com.tedu.ds.workRecord.mapper.WorkRecordMapper;
import com.tedu.ds.workRecord.pojo.dto.WorkRecordParam;
import com.tedu.ds.workRecord.pojo.dto.WorkRecordQuery;
import com.tedu.ds.workRecord.pojo.entity.WorkRecord;
import com.tedu.ds.workRecord.pojo.vo.WorkRecordVO;
import com.tedu.ds.workRecord.service.WorkRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WorkRecordServiceImpl implements WorkRecordService {
    @Autowired
    private WorkRecordMapper workRecordMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<WorkRecordVO> select(WorkRecordQuery workRecordQuery) {
        List<WorkRecordVO> workRecordVOS=workRecordMapper.select(workRecordQuery);
        if (!workRecordVOS.isEmpty()){
            for (WorkRecordVO w:workRecordVOS){
                if (w.getEmpId()!=null){
                    EmployeeQuery employeeQuery=new EmployeeQuery();
                    employeeQuery.setId(w.getEmpId());
                    List<EmployeeVO> select = employeeMapper.select(employeeQuery);
                    if (!select.isEmpty()){
                        w.setEmpName(select.get(0).getUsername());
                    }else{
                        w.setEmpName("不存在");
                    }
                }
            }
        }

        return workRecordVOS;
    }

    @Override
    public void save(WorkRecordParam workRecordParam) {
        WorkRecord workRecord=new WorkRecord();
        BeanUtils.copyProperties(workRecordParam,workRecord);
        if (workRecord.getId()==null){
            workRecordMapper.insert(workRecord);
        }else {
            workRecordMapper.update(workRecord);
        }
    }

    @Override
    public void deleteById(Integer id) {
        workRecordMapper.deleteById(id);
    }
}
